﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using H2.NET.Controls;
using H2.NET.Data.Common;
using H2.NET.Business;

namespace H2.NET.Web.Pages
{
    public partial class TabInsQual : TaskControl
    {
        #region 属性 Public
        public MDCTabStatus Status
        {
            get
            {
                if (StoreAPI["Status~" + Key] == null)
                {
                    StoreAPI["Status~" + Key] = new MDCTabStatus();
                }

                return (MDCTabStatus)StoreAPI["Status~" + Key];
            }
            set
            {
                Key = value.KeyWord;
                StoreAPI["Status~" + Key] = value;
            }
        }

        public string Key
        {
            get
            {
                return ViewState["Key"] == null ? "" : (string)ViewState["Key"];

            }
            set { ViewState["Key"] = value; }
        }

        public int SheetIndex
        {
            get
            {
                return ViewState["SheetIndex"] == null ? 0 : (int)ViewState["SheetIndex"];

            }
            set { ViewState["SheetIndex"] = value; }
        }
        #endregion

        #region 事件 Load (页面)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            InitDetails();

            if (!Page.IsPostBack)
            {
                InitControls();
            }
         }

        public void InitControls()
        {
            if (Status.Category == "INSP.QUAL")
            {
                IList<mdc_Ins_Qual> ins_qual = (List<mdc_Ins_Qual>)gv_Ins_Qual.DataSource;

                if (!Page.IsPostBack)
                {
                    gv_Ins_Qual.Key = Key;
                    Qual_P11.Visible = true;

                    #region 发起步骤
                    if (TaskAPI.IsInitiate)
                    {
                        List<mdc_Ins_Qual> ins_qual_s = new List<mdc_Ins_Qual>();
                        for (int i = 0; i < 4; i++)
                        {
                            ins_qual_s.Add(new mdc_Ins_Qual());
                            ins_qual_s[i].No = i + 1;
                            if (TaskAPI.Process == "TW Inspection Plan Maintenance")
                            {
                                ins_qual_s[i].InfoField_3 = "0";
                                ins_qual_s[i].Search_Field = "0";
                                ins_qual_s[i].Plant = "";
                            }
                        }
                        gv_Ins_Qual.DataSource = ins_qual_s;
                    }
                    #endregion

                    #region 审批环节
                    else
                    {
                        IList<mdc_Ins_Qual> ins_qual_s = (mdc_Ins_Qual.process == TaskAPI.Process & mdc_Ins_Qual.incident == TaskAPI.Incident).List<mdc_Ins_Qual>();
                        if (ins_qual_s.Count == 0)
                        {
                            for (int i = 0; i < 4; i++)
                            {
                                ins_qual_s.Add(new mdc_Ins_Qual());
                                ins_qual_s[i].No = i + 1;
                                if (TaskAPI.Process == "TW Inspection Plan Maintenance")
                                {
                                    ins_qual_s[i].Plant = "";
                                    ins_qual_s[i].InfoField_3 = "0";
                                    ins_qual_s[i].Search_Field = "0";
                                }
                            }
                        }
                        gv_Ins_Qual.DataSource = ins_qual_s;

                    }
                    #endregion
                }
            }
        }

        #endregion

        #region 事件 Add(增加行)
        protected void Add_Click(object sender, EventArgs e)
        {
            switch (((Anthem.Button)sender).ID)
            {
                case "Ins_Qual_Add":
                    List<mdc_Ins_Qual> ins_qual_s = (List<mdc_Ins_Qual>)gv_Ins_Qual.DataSource;
                    mdc_Ins_Qual ins_qual = new mdc_Ins_Qual();
                    ins_qual.No = ins_qual_s.Count + 1;
                    if (TaskAPI.Process == "TW Inspection Plan Maintenance")
                    {
                        ins_qual.Plant = "";
                        ins_qual.InfoField_3 = "0";
                        ins_qual.Search_Field = "0";
                    }
                    ins_qual_s.Insert(gv_Ins_Qual.ChangedRowIndex == -1 || gv_Ins_Qual.ChangedRowIndex >= ins_qual_s.Count ? ins_qual_s.Count - 1 : gv_Ins_Qual.ChangedRowIndex, ins_qual);
                    gv_Ins_Qual.DataSource = ins_qual_s;
                    gv_Ins_Qual.DataBind();
                    break;
            }
        }
        #endregion

        #region 方法 InitDetails(初始化明细行)
        public void InitDetails()
         {
            bool flg = Status.Technical == "C" && TaskAPI.IsAllowSubmit;
            if (TaskAPI.Process == "TW Inspection Plan Maintenance")
            {
                SetVisible(Status.Technical == "C", Inspection_Catalogs_L, Inspection_Catalogs, Control_Indicators_L, Control_Indicators, Preset_indicators_L, Preset_indicators, Status1_L, Status1);
                gv_Ins_Qual.DynamicColumn.Add(new BoundColumn2("00", "No", BoundColumnType2.TextBox, 0, Align.Left, true, "No", InputType.None, null));
                gv_Ins_Qual.DynamicColumn.Add(new BoundColumn2("01", "Plant", BoundColumnType2.DropDownList, 60, Align.Left, flg, "Plant", InputType.None, new string[3] { "", "KT1P", "KT2P" }));
                gv_Ins_Qual.DynamicColumn.Add(new BoundColumn2("02", "Old Name", BoundColumnType2.TextBox, 150, Align.Left, flg, "Valid_From", InputType.None, null));
                gv_Ins_Qual.DynamicColumn.Add(new BoundColumn2("03", "SAP Characteristic Code", BoundColumnType2.TextBox, 200, Align.Left, flg, "Characteristic_Code", InputType.Numeric, null));
                gv_Ins_Qual.DynamicColumn.Add(new BoundColumn2("04", "Type", BoundColumnType2.DropDownList, 100, Align.Left, flg, "Type", InputType.None, new string[4] { "", "Creation", "Revision", "Deletion" }));
                gv_Ins_Qual.DynamicColumn.Add(new BoundColumn2("05", "Short text", BoundColumnType2.TextBox, 150, Align.Left, flg, "Short_Text", InputType.None, null));
                gv_Ins_Qual.DynamicColumn.Add(new BoundColumn2("06", "Description(ZF)", BoundColumnType2.TextBox, 200, Align.Left, flg, "Description_ZH", InputType.None, null));
                gv_Ins_Qual.DynamicColumn.Add(new BoundColumn2("07", "Search field", BoundColumnType2.Label, 150, Align.Left, flg, "Search_Field", InputType.None, "0"));
                gv_Ins_Qual.DynamicColumn.Add(new BoundColumn2("08", "InfoField 3", BoundColumnType2.Label, 200, Align.Left, flg, "InfoField_3", InputType.None, "0"));
               
            }
            else
            {
                gv_Ins_Qual.DynamicColumn.Add(new BoundColumn2("00", "No", BoundColumnType2.TextBox, 0, Align.Left, true, "No", InputType.None, null));
                gv_Ins_Qual.DynamicColumn.Add(new BoundColumn2("01", "Plant", BoundColumnType2.DropDownList, 60, Align.Left, false, "Plant", InputType.None, null));
                gv_Ins_Qual.DynamicColumn.Add(new BoundColumn2("02", "Valid From</br>(默认为DMO创建日期)", BoundColumnType2.Calender, 90, Align.Left, flg, "Valid_From", InputType.None, null));
                gv_Ins_Qual.DynamicColumn.Add(new BoundColumn2("03", "Characteristic Code", BoundColumnType2.TextBox, 90, Align.Left, flg, "Characteristic_Code", InputType.Numeric, null));
                gv_Ins_Qual.DynamicColumn.Add(new BoundColumn2("04", "Type", BoundColumnType2.DropDownList, 80, Align.Left, flg, "Type", InputType.None, new string[4] { "", "Creation", "Revision", "Deletion" }));
                gv_Ins_Qual.DynamicColumn.Add(new BoundColumn2("05", "Short text", BoundColumnType2.TextBox, 60, Align.Left, flg, "Short_Text", InputType.None, null));
                gv_Ins_Qual.DynamicColumn.Add(new BoundColumn2("06", "Description(ZH)", BoundColumnType2.TextBox, -1, Align.Left, flg, "Description_ZH", InputType.None, null));
                gv_Ins_Qual.DynamicColumn.Add(new BoundColumn2("07", "Search field", BoundColumnType2.TextBox, 60, Align.Left, flg, "Search_Field", InputType.None, null));
                gv_Ins_Qual.DynamicColumn.Add(new BoundColumn2("08", "InfoField 3", BoundColumnType2.TextBox, 60, Align.Left, flg, "InfoField_3", InputType.None, null));
                gv_Ins_Qual.DynamicColumn.Add(new BoundColumn2("09", "CGr/Sset", BoundColumnType2.TextBox, 60, Align.Left, false, "CGr_Sset", InputType.Numeric, null));
                gv_Ins_Qual.DynamicColumn.Add(new BoundColumn2("10", "Plant", BoundColumnType2.DropDownList, 60, Align.Left, false, "InsQual_Plant", InputType.None, null));
                gv_Ins_Qual.DynamicColumn.Add(new BoundColumn2("11", "Status", BoundColumnType2.DropDownList, 70, Align.Left, false, "InsQual_Status", InputType.None, new string[6] { "", "1, Being Created", "2, Released", "3, Can no longer be", "4, Deletion flag", "5, Archived" }));
            }
            SetEnable(flg, Ins_Qual_Add);
            SetEnable(flg, Ins_Qual_Delete);
        }
        #endregion  

        #region 事件 Delete(删除行)
        protected void Delete_Click(object sender, EventArgs e)
        {
            switch (((Anthem.Button)sender).ID)
            {
                case "Ins_Qual_Delete":
                    List<mdc_Ins_Qual> ins_qual_s = (List<mdc_Ins_Qual>)gv_Ins_Qual.DataSource;
                    IList<mdc_Ins_Qual> list = (mdc_Ins_Qual.process == TaskAPI.Process & mdc_Ins_Qual.incident == TaskAPI.Incident).List<mdc_Ins_Qual>();
                    if (ins_qual_s.Count == 1)
                    {
                        Anthem.Manager.AddScriptForClientSideEval("JSInfomation('至少保留最后一个 Sheet ！',false)");
                        return;
                    }
                    ins_qual_s.RemoveAt(gv_Ins_Qual.ChangedRowIndex == -1 || gv_Ins_Qual.ChangedRowIndex >= ins_qual_s.Count ? ins_qual_s.Count - 1 : gv_Ins_Qual.ChangedRowIndex);
                    bool flag = false;
                    foreach (mdc_Ins_Qual ins_qual_1 in list)
                    {
                        if (string.IsNullOrEmpty(ins_qual_1.Characteristic_Code))
                        {
                            continue;
                        }
                        foreach (mdc_Ins_Qual ins_qual_2 in ins_qual_s)
                        {

                            if (string.IsNullOrEmpty(ins_qual_1.Characteristic_Code))
                            {
                                continue;
                            }
                            if (ins_qual_2.ID == ins_qual_1.ID)
                            {
                                flag = true;
                                break;
                            }
                        }
                        if (!flag)
                        {
                            Storage.Delete(ins_qual_1);
                        }
                        flag = false;
                    }

                    gv_Ins_Qual.DataSource = ins_qual_s;
                    gv_Ins_Qual.DataBind();
                    break;
            }
        }
        #endregion
        
        #region 方法 Save(保存数据库)
        public void Save()
        {
            if (!Status.IsEdited)
            {
                return;
            }

            switch (Status.Category)
            {

                #region QUAL
                case "INSP.QUAL":
                    foreach (mdc_Ins_Qual ins_qual in (List<mdc_Ins_Qual>)gv_Ins_Qual.DataSource)
                    {
                        if (string.IsNullOrEmpty(ins_qual.Short_Text))
                        {
                            continue;
                        }
                        ins_qual.Process = TaskAPI.Process;
                        ins_qual.Incident = TaskAPI.Incident;
                        ins_qual.UserName = UserAPI.UserName;
                        ins_qual.Time = System.DateTime.Now;
                        ins_qual.Status = 0;
                        Storage.Save(ins_qual);
                    }
                    break;
                #endregion
            }
        }
        #endregion

        #region 方法 DataValidation(数据验证)
        public List<string> DataValidation()
        {
            //如果当前 Tabs 是隐藏的情况，退出
            //if (!Status.IsEdited)
            //{
            //    return new List<string>();
            //}

            List<string> vald_s = new List<string>();
            List<Regular> regs = new List<Regular>();

            //if (Status.Category == "MTO-SEFI")
            //{
            //    regs.Add(new Regular(RegularExp.NotEmpty,  MTO_SEFI_Base_Quantity.Text, "&nbsp;&nbsp;Base Quantity 为必填"));
            //    regs.Add(new Regular(RegularExp.NotEmpty, MTO_SEFI_Plant.Text, "&nbsp;&nbsp;Plant 为必填"));
            //    regs.Add(new Regular(RegularExp.NotEmpty, MTO_SEFI_MTO_Code.Text, "&nbsp;&nbsp;MTO Code 为必填"));

            //    foreach (mdc_Bom_MTO_Sefi_2 mto_sefi in (List<mdc_Bom_MTO_Sefi_2>)gv_MTO_SEFI.DataSource)
            //    {
            //        if (string.IsNullOrEmpty(mto_sefi.Ict) && string.IsNullOrEmpty(mto_sefi.Routing_Text) && string.IsNullOrEmpty(mto_sefi.Component_SAP_Code))
            //        {
            //            continue;
            //        }

            //        if (!string.IsNullOrEmpty(mto_sefi.Ict) && mto_sefi.Ict == "L")
            //        {
            //            mto_sefi.Routing_Text = "";
            //            regs.Add(new Regular(RegularExp.NotEmpty, mto_sefi.Component_SAP_Code, "&nbsp;&nbsp;行" + mto_sefi.No + ": Component SAP Code 为必填"));
            //            regs.Add(new Regular(RegularExp.NotEmpty, mto_sefi.Quantity, "&nbsp;&nbsp;行" + mto_sefi.No + ": Quantity 为必填"));
            //            regs.Add(new Regular(RegularExp.NotEmpty, mto_sefi.Component_Description, "&nbsp;&nbsp;行" + mto_sefi.No + ": Component Description 为必填"));
            //        }

            //        if (!string.IsNullOrEmpty(mto_sefi.Ict) && mto_sefi.Ict == "T")
            //        {
            //            mto_sefi.Component_Description = "";
            //            mto_sefi.Component_SAP_Code = "";
            //            regs.Add(new Regular(RegularExp.NotEmpty, mto_sefi.Routing_Text, "&nbsp;&nbsp;行" + mto_sefi.No + ": Routing Text为必填"));

            //        }
            //    }

            //    if (string.IsNullOrEmpty(((List<mdc_Bom_MTO_Sefi_2>)gv_MTO_SEFI.DataSource)[0].Ict))
            //    {
            //        regs.Add(new Regular(RegularExp.NotEmpty, "", "&nbsp;&nbsp;行" + "BOM: 明细行中至少填写 1 行数据"));
            //    }
            if (TaskAPI.IsBeginStep && TaskAPI.Process == "TW Inspection Plan Maintenance")
            {
                if ((Page as PMDC).Qual_Selected.Checked && (Page as PMDC).Qual_Selected.Visible)
                {
                    foreach (mdc_Ins_Qual ccc in (List<mdc_Ins_Qual>)this.gv_Ins_Qual.DataSource)
                    {
                        if (string.IsNullOrEmpty(ccc.Plant) && string.IsNullOrEmpty(ccc.Characteristic_Code) && string.IsNullOrEmpty(ccc.Type) && string.IsNullOrEmpty(ccc.Short_Text) && string.IsNullOrEmpty(ccc.Description_ZH))
                        {
                            continue;
                        }
                        regs.Add(new Regular(RegularExp.NotEmpty, ccc.Plant, "&nbsp;&nbsp;  表单 行" + ccc.No + ": Plant为必填"));
                        regs.Add(new Regular(RegularExp.NotEmpty, ccc.Characteristic_Code, "&nbsp;&nbsp;  表单 行" + ccc.No + ": SAP Characteristic Code为必填"));
                        regs.Add(new Regular(RegularExp.NotEmpty, ccc.Type, "&nbsp;&nbsp;  表单 行" + ccc.No + ": Type为必填"));
                        regs.Add(new Regular(RegularExp.NotEmpty, ccc.Short_Text, "&nbsp;&nbsp;  表单 行" + ccc.No + ": Short text为必填"));
                        regs.Add(new Regular(RegularExp.NotEmpty, ccc.Description_ZH, "&nbsp;&nbsp;  表单 行" + ccc.No + ": Description(ZF)为必填"));

                    }
                }
            }
            vald_s.AddRange(Validation(regs));

            if (vald_s.Count > 0)
            {
                vald_s.Insert(0, "<font color='#0000FF'><b>" + Status.Name + "</b></font>");
                vald_s.Add("&nbsp;&nbsp;-----------------------------------------");
            }

            return vald_s;
        }
        #endregion

        #region 方法 gv_InsQual 数据取得
        protected void gv_Ins_Qual_DataBinding(object sender, EventArgs e)
        {
            if (gv_Ins_Qual.ChangedColumnIndex == 4 || gv_Ins_Qual.ChangedColumnIndex == 5 || gv_Ins_Qual.ChangedColumnIndex == 6 || gv_Ins_Qual.ChangedColumnIndex == 7)
            {
                List<mdc_Ins_Qual> ins_qual_s = gv_Ins_Qual.DataSource as List<mdc_Ins_Qual>;
                //if (gv_Ins_Qual.ChangedRowIndex >= 0 && gv_Ins_Qual.ChangedRowIndex < ins_qual_s.Count)
                //{
                    ins_qual_s[gv_Ins_Qual.ChangedRowIndex].Search_Field = ins_qual_s[gv_Ins_Qual.ChangedRowIndex].Short_Text;
                    ins_qual_s[gv_Ins_Qual.ChangedRowIndex].InfoField_3 = ins_qual_s[gv_Ins_Qual.ChangedRowIndex].Short_Text;
                //}

                gv_Ins_Qual.DataSource = ins_qual_s;
            }
        }
        #endregion
    }
}
